<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastError, ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import configForm from './configForm.vue';
  import xmImage from '@/components/xmImage/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  const configFormRef = ref();
  const action = reactive({
    total:0,
    list:[] as any[],
    gameStatus:-1, // 1开始 2关闭
    gameDamond:0, // 钻石限制数量
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    search_id:'',
    kwd:'',
    status:1,
  })
  const stateList = [
    {key:1,label:'显示'},
    {key:2,label:'不显示'},
  ]
  const resetQuery = ()=>{
    Object.assign(listQuery,{
      page:1,
      limit:20,
      search_id:'',
      kwd:'',
      status:1
    })
    getList()
  }
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const handleGameSet = ()=>{
    if(action.gameStatus == 2) {
      ElMessageBox.prompt('', '限制钻石数', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern:/^[0-9]*[1-9][0-9]*$/,
        inputPlaceholder:'请输入正整数限制钻石数',
        inputErrorMessage: '请输入正整数限制钻石数',
      })
      .then(async({ value }) => {
        action.gameDamond = Number(value)
        setGameStatus(1)
      }).catch(() => { })
    }
    if(action.gameStatus == 1) {
      ElMessageBox.confirm(`是否关闭游戏?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          setGameStatus(2)
        })
        .catch(() => { });
    }
  }
  const setGameStatus = (status:number = 0)=>{
    if(!status) return ToastWarning('status not defint')
    const loading = Toastloading();
    gamesApi.gameOpen({
      status,
      diamond:action.gameDamond
    }).then(()=>{
      ToastSuccess("操作成功!");
      getGameOpeninfo()
    }).catch(()=>{
      loading.close();
    })
  }
  const handleDel = (row:any = {})=>{
    const { id=0, game_name=''} = row
    if(!id) return ToastError('id not defint')
    ElMessageBox.confirm(`是否删除${game_name}?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        const loading = Toastloading();
        gamesApi.delGamePages({
          id,
        }).then(()=>{
          ToastSuccess("删除成功!");
          getList();
        }).catch(()=>{
          loading.close();
        })
        
      })
      .catch(() => { });
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await gamesApi.getGamePages(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  const getGameOpeninfo = async()=>{
    const loading = Toastloading()
    const res = await gamesApi.getGameOpeninfo()
    const { status=2, diamond=0 } = res?.data 
    action.gameStatus = status
    action.gameDamond = diamond
    loading.close()
  }
  const handleForm = (row:any = {})=>{
    configFormRef.value.openDialog({
      ...row,
      stateList
    })
  }
  onMounted(()=>{
    getGameOpeninfo()
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">ID：</div>
          <el-input v-model="listQuery.search_id" placeholder="请输入ID"/>
        </div>
        <div class="labelItem">
          <div class="label">名称：</div>
          <el-input v-model="listQuery.kwd" placeholder="请输入名称"/>
        </div>
        <div class="labelItem">
          <div class="label">展示状态：</div>
          <el-select v-model="listQuery.status" clearable placeholder="请选择展示状态">
            <el-option
              v-for="item in stateList"
              :key="item.key"
              :label="item.label"
              :value="item.key"
            />
          </el-select>
        </div>
      </div>
      <div class="labelBox">
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleForm()">新增</el-button>
          <el-button
            v-if="action.gameStatus > -1"
            :type="['','danger','success'][action.gameStatus]"
            @click="handleGameSet">{{ ['','关闭游戏','开启游戏'][action.gameStatus] }}</el-button>
        </div>
      </div>
    </div>
    <div class="labelBox TablelabelBox">
      <div class="labelItem">
        <div class="label">限制钻石数：</div>{{ action.gameDamond }}钻石
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column width="80" prop="sort" label="排序" />
      <el-table-column width="100" prop="id" label="ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="game_name" label="名称" />
      <el-table-column width="100" prop="diamond_value" label="钻石限制" />
      <el-table-column width="100" prop="status" label="展示状态">
        <template #default="scope">
          <el-tag>{{ ['','显示','不显示'][scope.row.status] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="140" prop="default_url" label="默认图片">
        <template #default="scoped">
          <xmImage :src="scoped.row.default_url" :list="[scoped.row.default_url]" />
        </template>
      </el-table-column>
      <el-table-column width="140" prop="icon_url" label="icon缩略图">
        <template #default="scoped">
          <xmImage :src="scoped.row.icon_url" :list="[scoped.row.icon_url]" />
        </template>
      </el-table-column>
      <el-table-column min-width="140" prop="video_url" label="视频">
        <template #default="scoped">
          <xmImage :src="scoped.row.video_url" :list="[scoped.row.video_url]" />
        </template>
      </el-table-column>
      <el-table-column min-width="120" show-overflow-tooltip prop="jump_url" label="跳转链接" />
      <el-table-column width="100" prop="scale" label="视频比例" />
      <el-table-column fixed="right" width="180" label="操作">
        <template #default="scope">
          <el-button @click="handleForm(scope.row)" type="primary">修改</el-button>
          <el-button @click="handleDel(scope.row)" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <configForm @update-list="getList" ref="configFormRef" />
  </div>
</template>